<template>
  <section class="todoapp">
    <TodoHeader :arr="list" @add="addFn"></TodoHeader>
    <TodoMain :arr="islist" @del="delFn"></TodoMain>
    <TodoFooter :arr="list" @clear="clearFn" @change="changeFn"></TodoFooter>
  </section>
</template>

<script>
// 样式引入 + 导入组件并注册使用
import TodoHeader from "./components/TodoHeader.vue";
import TodoMain from "./components/TodoMain.vue";
import TodoFooter from "./components/TodoFooter.vue";
import "./styles/base.css";
import "./styles/index.css";
export default {
  data() {
    return {
      list: JSON.parse(localStorage.getItem("plist")) || [],
      type: "all",
    };
  },
  components: {
    TodoHeader,
    TodoMain,
    TodoFooter,
  },
  methods: {
    delFn(id) {
      this.list = this.list.filter((item) => item.id !== id);
    },
    addFn(name) {
      this.list.unshift({
        id: +new Date(),
        name,
        isDone: false,
      });
    },
    clearFn() {
      this.list = this.list.filter((item) => !item.isDone);
    },
    changeFn(type) {
      this.type = type;
    },
  },
  computed: {
    islist() {
      if (this.type === "ndone") {
        return this.list.filter((item) => !item.isDone);
      } else if (this.type === "done") {
        return this.list.filter((item) => item.isDone);
      } else {
        return this.list;
      }
    },
  },
  watch: {
    list: {
      deep: true,
      handler() {
        localStorage.setItem("plist", JSON.stringify(this.list));
      },
    },
  },
};
</script>
